package com.wujie.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.wujie.market.dao.GoodsDao;
import com.wujie.market.po.Goods;
import com.wujie.market.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: GoodsServiceIml
 * Description:
 * date: 2023/1/27 19:09
 *
 * @author 吴杰
 * @since JDK 1.8.0_31
 */
@Service
public class GoodsServiceImp implements GoodsService {
    @Resource
    private GoodsDao goodsDao;


    @Override
    public List<Goods> querylist() {
        return goodsDao.selectGoodsList();
    }

    @Override
    public List<Goods> listPlus(String gType,String gName) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (!gType.equals("")) {
            queryWrapper.eq("g_type",gType);
    }
        if (!gName.equals("")) {
            queryWrapper.like("g_name",gName);
}
        return goodsDao.selectList(queryWrapper);
    }

    @Override
    public Goods queryById(int id) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("g_id",id);
        return goodsDao.selectOne(queryWrapper);
    }

    @Override
    public int savaGoods(Goods goods) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper();
        wrapper.eq("g_id",goods.getgId());
        return goodsDao.update(goods,wrapper);
    }

    @Override
    public int insertGoods(Goods goods) {
        int result = goodsDao.insert(goods);
        return result;
    }

    @Override
    public List<Goods> queryWarnGoods() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.le("cur_num",50);
        return goodsDao.selectList(wrapper);
    }

    @Override
    public List<Goods> queryMaxSale() {
        QueryWrapper<Goods> querywrapper = new QueryWrapper<>();
        querywrapper.orderByDesc("sale_num").last("limit 5");
        return goodsDao.selectList(querywrapper);
    }
}
